package com.zsr.xmall.member.controller;

import com.zsr.common.exception.BizCodeEnum;
import com.zsr.common.to.member.MemberUserLoginTO;
import com.zsr.common.to.member.MemberUserRegisterTO;
import com.zsr.common.utils.PageUtils;
import com.zsr.common.utils.R;
import com.zsr.common.vo.auth.BaseSocialUser;
import com.zsr.xmall.member.entity.MemberEntity;
import com.zsr.xmall.member.exception.PhoneExistException;
import com.zsr.xmall.member.exception.UserNameExistException;
import com.zsr.xmall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author zsrey
 * @email zsrey8880@163.com
 * @date 2022-06-09 23:28:54
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 注册
     *
     * @param userRegisterTo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberUserRegisterTO userRegisterTo) {
        try {
            memberService.register(userRegisterTo);
            return R.ok();
        } catch (PhoneExistException ex) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION);
        } catch (UserNameExistException ex) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberUserLoginTO userLoginTo) {
        MemberEntity member = memberService.login(userLoginTo);
        return member == null ? R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION) : R.ok().setData(member);
    }

    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody BaseSocialUser baseSocialUser) {
        try {
            MemberEntity memberEntity = memberService.login(baseSocialUser);
            return R.ok().setData(memberEntity);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

}
